/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jdh;
import java.sql.*;

/**
 *
 * @author tale
 */
public class Backend extends ServerConfig {

    public static Backend getDefaultBackend(){
        if (defaultBackend == null){
            defaultBackend = new Backend();
            defaultBackend.setDefault();
            defaultBackend.connect();
        }
        return defaultBackend;
    }
    
    static Backend defaultBackend = null;
    Connection con = null;
    String[] userType2Table = {"ADMINS", "TEACHERS", "STUDENTS"};

    public boolean test(){
        String sql[] = {
            "SELECT * FROM PLANS",
            "SELECT * FROM TEACHERS",
            "SELECT * FROM STUDENTS",
            "SELECT * FROM LECTURES",
            "CREATE TABLE _TEST (ID INT)",
            "DROP TABLE _TEST"
        };
        boolean value = false;
        try{
            if (connect() == true){
                for (int i = 0; i < sql.length; i++){
                    Statement st = con.createStatement();
                    st.execute(sql[i]);
                    st.close();
                    System.out.println("TEST " + i + " passed.");
                }
                value = true;
            }
        }catch(SQLException e){
            System.out.println(e.toString());
        }finally{
            disconnect();
        }
        return value;
    }

    public boolean connect(){
        disconnect();
        try{
            Class.forName(getDriver());
        }catch(ClassNotFoundException e){
            System.out.println(" Not Found Driver:" + getDriver());
            return false;
        }
        try{
            con = DriverManager.getConnection(getUrl(), username, password);
        }catch(SQLException e){
            System.out.println("Failed to connect to server.");
            System.out.println(e.toString());
            return false;
        }
        return true;
    }

    public boolean disconnect(){
        try{con.close();}catch(Exception e){}
        con = null;
        return true;
    }

    public Connection getConnection(){
        if (con == null)
            connect();
        return con;
    }

    public boolean checkToLogin(String id, char[] passwordValue, int userType){
        String password = String.valueOf(passwordValue);
        boolean value = false;
        String query =String.format("SELECT PASSWORD FROM %S " +
                "WHERE ID = '%S' AND PASSWORD = '%S' ",
                userType2Table[userType], id, password);
        try{
            if (connect() == true){
                Statement st = con.createStatement();
                ResultSet rs= st.executeQuery(query);
                if (rs.next()){
                    value = true;
                }
                st.close();
                rs.close();
            }
        }catch(SQLException e){
            System.out.println(e.toString());
        }
        return value;
    }

    public static void main(String[] args){
        Backend backend = Backend.getDefaultBackend();
        backend.test();
        System.out.println(backend.checkToLogin("1", "000000".toCharArray(), 0));
        System.out.println(backend.checkToLogin("1", "01230".toCharArray(), 0));
    }
}

